package com.bajiao.admin.controller.message;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bajiao.admin.base.AdminBaseController;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.aspect.annotation.AutoLog;
import com.bajiao.entity.message.Message;
import com.bajiao.service.message.MessageService;
import com.bajiao.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 消息表
 * @Author: FanJq
 * @Date: 2024-10-10
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "消息表")
@RestController
@RequestMapping("/message")
public class MessageController extends AdminBaseController<Message, MessageService> {

    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;

    /**
     * 分页列表查询
     *
     * @param message
     * @param
     * @return
     */
    @AutoLog(value = "消息表-分页列表查询")
    @ApiOperation(value = "消息表-分页列表查询", notes = "消息表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(Message message) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(message.getTitle())) {
            queryWrapper.like(Message::getTitle, message.getTitle());
        }
        queryWrapper.eq(Message::getDelFlag, 0).orderByDesc(Message::getCreateTime);
        startPage();
        return Result.ok(getDataTable(messageService.list(queryWrapper)));
    }
    /**
     * 添加
     *
     * @param message
     * @return
     */
    @AutoLog(value = "消息表-添加")
    @ApiOperation(value = "消息表-添加", notes = "消息表-添加")
    @PostMapping(value = "/add")
    public Result<Message> add(@RequestBody Message message) {
        Result<Message> result = new Result<Message>();
        try {
            messageService.save(message);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param message
     * @return
     */
    @AutoLog(value = "消息表-编辑")
    @ApiOperation(value = "消息表-编辑", notes = "消息表-编辑")
    @PutMapping(value = "/edit")
    public Result<Message> edit(@RequestBody Message message) {
        Result<Message> result = new Result<Message>();
        Message messageEntity = messageService.getById(message.getId());
        if (messageEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = messageService.updateById(message);
//            pushMessage(message);
            //TODO 返回false说明什么？
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "消息表-通过id删除")
    @ApiOperation(value = "消息表-通过id删除", notes = "消息表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        messageService.update(new LambdaUpdateWrapper<Message>().eq(Message::getId, id).set(Message::getDelFlag, 1)
                .set(Message::getUpdateTime, new Date()));
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "消息表-批量删除")
    @ApiOperation(value = "消息表-批量删除", notes = "消息表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<Message> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        Result<Message> result = new Result<Message>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            List<String> idList = Arrays.asList(ids.split(","));
            this.messageService.removeByIds(idList);
            result.success("删除成功!");
        }
        return result;
    }
}
